/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package soft.hb.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSONObject;

import soft.hb.R;
import soft.hb.application.HbApplication;
import soft.hb.model.User;
import soft.hb.util.HttpRequest;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseModel;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.util.JSON;
import zuo.biao.library.util.Log;

/**联系人资料界面
 * @author Lemon
 */
public class LoginActivity extends BaseActivity implements OnClickListener, OnHttpResponseListener {
	public static final String TAG = "LoginActivity";

	/**获取启动LoginActivity的intent
	 * @param context
	 * @return
	 */
	public static Intent createIntent(Context context) {
		return new Intent(context, LoginActivity.class);
	}


	@Override
	public Activity getActivity() {
		return this; //必须return this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);
		intent = getIntent();

		//功能归类分区方法，必须调用<<<<<<<<<<
		initView();
		initData();
		initEvent();
		//功能归类分区方法，必须调用>>>>>>>>>>

	}


	//UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


	private RelativeLayout layLoginBackground;
	private EditText editUName;
	private EditText editPwd;
	private Button btnLogin;

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	public void initView() {//必须调用
		layLoginBackground = findView(R.id.lay_login_background);
//		showBlurBackground(layLoginBackground);
		editUName = findView(R.id.edit_uname);
		editPwd = findView(R.id.edit_pwd);
		btnLogin = findView(R.id.btn_login);

	}




	//UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>










	//Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


	@Override
	public void initData() {//必须调用


	}

	//Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>








	//Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	@Override
	public void initEvent() {//必须调用
		btnLogin.setOnClickListener(this);
	}


	//对应HttpRequest.getUser(userId, 0, UserActivity.this); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	@Override
	public void onHttpResponse(int requestCode, String resultJson, Exception e) {

	}

	//系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			String uname = editUName.getText().toString();
			String pwd = editPwd.getText().toString();
			if(TextUtils.isEmpty(uname)|| TextUtils.isEmpty(pwd)){
				showShortToast("用户名密码不能为空");
				return;
			}
			showProgressDialog(R.string.logging);
			HttpRequest.login(uname, pwd, 0, new OnHttpResponseListener() {
				@Override
				public void onHttpResponse(int requestCode, String resultJson, Exception e) {
					User user = null;
					try {//如果服务器返回的json一定在最外层有个data，可以用OnHttpResponseListenerImpl解析
						dismissProgressDialog();
						JSONObject jsonObject = JSON.parseObject(resultJson);
						Log.i(TAG,jsonObject.toJSONString());
						if(jsonObject.getBoolean("success")){
							//登录成功
							showShortToast(R.string.login_succeed);
							JSONObject data = jsonObject == null ? null : jsonObject.getJSONObject("body");
							user = JSON.parseObject(data, User.class);
							if (BaseModel.isCorrect(user) == false && e != null) {
								showShortToast(R.string.get_failed);
							} else {
								HbApplication.getInstance().saveCurrentUser(user);
								startActivity(MainTabActivity.createIntent(LoginActivity.this));
								finish();
							}
						}else{
							showShortToast(jsonObject.getString("msg"));
						}


					} catch (Exception e1) {
						showShortToast(R.string.login_faild);
						dismissProgressDialog();
					}

				}
			});

			break;
		default:
			break;
		}
	}


	//类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	private static final int REQUEST_TO_BOTTOM_MENU = 1;
	private static final int REQUEST_TO_EDIT_TEXT_INFO = 2;

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case REQUEST_TO_BOTTOM_MENU:

			break;
		case REQUEST_TO_EDIT_TEXT_INFO:

			break;
		}
	}


	@Override
	public void finish() {
		super.finish();

	}




	//类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	//系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


	//Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}